import { Actions } from '../../core/utils/IConstant';

export default function homeReducer(state = {}, action)
{
    switch(action.type)
    {
    case Actions.HOME.GET_NEWS_REQUEST:
        return {...state, news: { isFetching: true }};
    case Actions.HOME.GET_NEWS_SUCCESS:
        return {...state, news: { isFetching: false, dataList: action.dataList }};
    case Actions.HOME.GET_NEWS_FAILURE:
        return {...state, news: { isFetching: false }};
    case Actions.HOME.GET_WEATHER_SUCCESS:
        return {...state, weather: action.resp};
    case Actions.HOME.SET_SCROLL_OFFSET:
        return {
            ...state, 
            scrollViewOffset: action.scrollViewOffset,
            playScrollViewOffset: action.playScrollViewOffset,
        };
    case Actions.HOME.NEWS_INCREASE_CLICK_COUNT:
        if(state && state.news && state.news.dataList && state.news.dataList.length > 0)
        {
            for(let i = 0; i < state.news.dataList.length; i++)
            {
                let item = state.news.dataList[i];
                if(item.ContentCode === action.code)
                {
                    if(!isNaN(item.ClickCount))
                    {
                        item.ClickCount += 1;
                        state.news.dataList = [...state.news.dataList];
                    }
                    break;
                }
            }
        }
        return {
            ...state
        };
    default:
        return state;
    }
}